{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 面向对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 传递类作为参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f in B\n"
     ]
    }
   ],
   "source": [
    "import abc\n",
    "class CLA(metaclass=abc.ABCMeta):\n",
    "    @staticmethod\n",
    "    def fun():\n",
    "        pass\n",
    "class CLB(CLA):\n",
    "    @staticmethod\n",
    "    def fun():\n",
    "        print(\"f in B\")\n",
    "\n",
    "class C():\n",
    "    def func(self, x):\n",
    "        x.fun()\n",
    "\n",
    "c = C()\n",
    "c.func(CLB)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 引用传参?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Wr:\n",
    "    def __init__(self,f,arg) -> None:\n",
    "        self.f = f\n",
    "        self.arg = arg\n",
    "\n",
    "    def callf(self):\n",
    "        self.f(self.arg)\n",
    "\n",
    "def f(argx):\n",
    "    argx['a'] = \"a\"\n",
    "\n",
    "arg1 = {\"b\":1}\n",
    "arg2 = {\"b\":1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b': 1, 'a': 'a'}\n"
     ]
    }
   ],
   "source": [
    "f(arg1)\n",
    "print(arg1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b': 1, 'a': 'a'}\n"
     ]
    }
   ],
   "source": [
    "w = Wr(f, arg2)\n",
    "w.callf()\n",
    "print(arg2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## hasattr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "class A:\n",
    "    def __init__(self) -> None:\n",
    "        pass\n",
    "    def fit():\n",
    "        self.x = True\n",
    "\n",
    "a = A()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "if hasattr(a, \"x\"):\n",
    "    print(\"has\")\n",
    "    print(a.x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参数inspect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import inspect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f1(df, a2, a3=\"a2v\", *args, **kwargs):\n",
    "    print(df, a2, a3, args, kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FullArgSpec(args=['df', 'a2', 'a3'], varargs='args', varkw='kwargs', defaults=('a2v',), kwonlyargs=[], kwonlydefaults=None, annotations={})\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# https://stackoverflow.com/a/218709/5142886\n",
    "arg_spec = inspect.getfullargspec(f1)\n",
    "print(arg_spec)\n",
    "print(type(arg_spec.defaults))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyWrapper:\n",
    "    def __init__(self, f, *args, **kwargs) -> None:\n",
    "        self.f = f\n",
    "        self.args = args\n",
    "        self.kwargs = kwargs\n",
    "\n",
    "    def process(self, df):\n",
    "        # print(self.args)\n",
    "        return self.f(df, *self.args, **self.kwargs)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df 2 a2v () {}\n",
      "df 2 a2v () {}\n",
      "df 2 3 () {}\n",
      "df 2 3 () {}\n",
      "df 2 3 (4,) {}\n",
      "df 2 3 (4,) {'a5': 5}\n",
      "df 2 a2v () {}\n",
      "df 2 a2v () {}\n",
      "df 2 3 () {}\n",
      "df 2 3 () {}\n",
      "df 2 3 (4,) {}\n",
      "df 2 3 (4,) {'a5': 5}\n"
     ]
    }
   ],
   "source": [
    "# 正常调用\n",
    "f1(\"df\", 2)\n",
    "# f1(a1=1, 2) #不行\n",
    "f1(\"df\", a2=2)\n",
    "f1(\"df\", 2, 3)\n",
    "f1(\"df\", 2, a3=3)\n",
    "f1(\"df\", 2, 3, 4)\n",
    "f1(\"df\", 2, 3, 4, a5=5)\n",
    "\n",
    "MyWrapper(f1, 2).process(\"df\")\n",
    "MyWrapper(f1, a2=2).process(\"df\")\n",
    "MyWrapper(f1, 2, 3).process(\"df\")\n",
    "MyWrapper(f1, 2, a3=3).process(\"df\")\n",
    "MyWrapper(f1, 2, 3, 4).process(\"df\")\n",
    "MyWrapper(f1, 2, 3, 4, a5=5).process(\"df\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df 2 3 () {'a': 'b'}\n"
     ]
    }
   ],
   "source": [
    "t = (\"df\", 2,3,)\n",
    "d = {\"a\": \"b\"}\n",
    "f1(*t, **d)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mlflow-dev-env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
